package com.haha.extend.button;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.CountDownTimer;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.Long.library.R;


/**
 * Created by longteng on 2016-6-20.
 */
public class CountDownButton extends Button{
    private Activity activity;
    private int startCountNum = 60,endCountNum = 0;
    private String defaultText,tickText,finishText;
    public CountDownButton(Context context) {
        this(context,null);
    }

    public CountDownButton(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public CountDownButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        if (context instanceof  Activity){
            this.activity  = (Activity) context;
        }else throw new IllegalAccessError("倒计时按钮只能在Activity或其子类中使用");
        getAttrs(attrs);
    }

    private void getAttrs(AttributeSet attrs){
        TypedArray ta = activity.obtainStyledAttributes(attrs, R.styleable.CountDownButton);
        startCountNum = ta.getInt(R.styleable.CountDownButton_startCountNum,endCountNum);
        endCountNum = ta.getInt(R.styleable.CountDownButton_endCountNum,endCountNum);
        defaultText = ta.getString(R.styleable.CountDownButton_defaultText);
        tickText = ta.getString(R.styleable.CountDownButton_tickText);
        finishText = ta.getString(R.styleable.CountDownButton_finishText);
        ta.recycle();
        init();
    }

    public void init(){
        final boolean decreases = startCountNum > endCountNum;
        final long totalMills = Math.abs(startCountNum - endCountNum) * 1000L;
        setText(defaultText);
        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("haha","clicked---------");
                new CountDownBtnThread(totalMills,1000,decreases).start();
                if (onButtonClickListener !=null) onButtonClickListener.onClick();
            }
        });
    }

    public interface OnButtonClickListener{
        void onClick();
    }
    private OnButtonClickListener onButtonClickListener;
    public void setOnButtonClickListener(OnButtonClickListener onButtonClickListener){
        this.onButtonClickListener = onButtonClickListener;
    }

    class CountDownBtnThread extends CountDownTimer {
        private int time;
        private boolean decrease;
        public CountDownBtnThread(long millisInFuture, long countDownInterval,boolean decrease) {
            super(millisInFuture, countDownInterval);
            this.decrease = decrease;
        }

        public CountDownBtnThread(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
            time = (int)(millisInFuture / countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            activity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (tickText.contains("%1$d")){
                        setText(String.format(tickText,(time = decrease ? time-1 : time + 1)));
                    }else setText(tickText);
                }
            });
        }

        @Override
        public void onFinish() {
            activity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    setText(finishText);
                    setEnabled(true);
                }
            });
        }
    }
}
